package gui;

import javax.swing.JPanel;

import Entities.RequestEntity;
import Entities.ReviewAndDecisionEntity;
import common.ChatIF;
import common.CommonMethods;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import java.awt.Font;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;

import javax.swing.JTextField;
import javax.swing.JScrollPane;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
/**
 * 
 * @author Idan Mann
 * This GUI Class services the committee members,
 * With it they have access to the request and it's assessment report.
 * This class also provides the committee with a discussion log including a date stamp and the member's name.
 *  *
 */
public class ReviewAndDecisionPanel extends JPanel implements ChatIF
{
	
	private ViewRequestPanel viewRequest;
	private ViewAssessmentPanel viewAssessment;
	private AcademicFrame myframe;
	private CommitteeMemberPanel committeePanel;
	private ReviewAndDecisionPanel myself;
	private JComboBox comboBox;
	private JTextArea discussionArea;
	private JTextArea recommendationArea;
	private JButton btnViewRequest;
	private JButton btnViewAssessment;
	private JButton btnSave;
	private JButton btnClose;
	private JButton btnVerdict;
	private JTextField deadlineArea;
	private JLabel lblDeadline;
	private JButton btnExtendDeadline;
	private String rid;
	private String assID;
	private String name;
	private boolean isChairman;
	
	private JButton btnReview;
	
	
	public ReviewAndDecisionPanel(AcademicFrame frame, CommitteeMemberPanel committeePanel, String name, boolean isChariman)
	{
		this.myframe = frame;
		this.committeePanel = committeePanel;
		this.name = name;
		this.isChairman=isChariman;
		myframe.setSize(500, 400);
		this.myself = this;
	
		initialzie();
		
	}
	
	private void initialzie()
	{
	
		setLayout(null);
		btnViewRequest = new JButton("View Request");
		btnViewRequest.setEnabled(false);
		btnViewRequest.setBounds(489, 176, 116, 23);
		myframe.setSize(500, 400);
		btnViewRequest.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				if (viewRequest==null)
				{
					viewRequest = new ViewRequestPanel(rid,myframe,myself,false,"");
					myframe.setContentPane(viewRequest);
				}
				else
					if (!viewRequest.getrequestID().equalsIgnoreCase(rid))
					{
						viewRequest = new ViewRequestPanel(rid,myframe,myself,false,"");
						myframe.setContentPane(viewRequest);
					}
				myframe.setContentPane(viewRequest);
			//	myframe.setSize(551, 730);	
				myframe.setSize(550, 730);						
			}
		});
		
		btnViewAssessment = new JButton("View Assessment");
		btnViewAssessment.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent arg1)
			{
				if (viewAssessment==null)
				{
					viewAssessment = new ViewAssessmentPanel(assID,myframe,myself);
					myframe.setContentPane(viewAssessment);
				}
				else
					if (!viewAssessment.getassessmentID().equalsIgnoreCase(assID))
					{
						viewAssessment = new ViewAssessmentPanel(assID,myframe,myself);
						myframe.setContentPane(viewAssessment);
					}
				myframe.setContentPane(viewAssessment);
				//myframe.setSize(551, 730);	
				myframe.setSize(550, 730);	
					
			}
		});
		
		add(btnViewRequest);
		
		JLabel lblReviewDecision = new JLabel("Review & Decision");
		lblReviewDecision.setFont(new Font("Tahoma", Font.PLAIN, 18));
		lblReviewDecision.setBounds(252, 37, 165, 14);
		add(lblReviewDecision);
		
		comboBox = new JComboBox();
		comboBox.addItemListener(new ItemListener() 
		{
			public void itemStateChanged(ItemEvent arg0)
			{
				btnReview.setEnabled(true);
				setButtons(false);
			}
		});
		comboBox.setBounds(226, 86, 165, 22);
		add(comboBox);
		
		JLabel lblRequest = new JLabel("Request to review");
		lblRequest.setBounds(92, 90, 136, 14);
		add(lblRequest);
		
		
		btnViewAssessment.setEnabled(false);
		btnViewAssessment.setBounds(489, 232, 117, 23);
		add(btnViewAssessment);
		
		btnClose = new JButton("Close");
		btnClose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e)
			{
				myframe.setSize(500, 500);
				myframe.setContentPane(committeePanel);					
			}
		});
		btnClose.setBounds(105, 460, 91, 23);
		add(btnClose);
		
		btnReview = new JButton("Review");
		btnReview.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e)
			{
				// Lock all buttons
				setButtons(false);
				
				rid = comboBox.getSelectedItem().toString(); // get selected request id
				if (rid.equalsIgnoreCase("--Choose a request--")) // filter invalid choice
					(new Dialog("Please choose an request from the list first.")).setVisible(true);
				else
				{
				
				// Get the assessment ID for the chosen request
				ArrayList<Object> convertRidIDtoAssid = new ArrayList<Object>();
				convertRidIDtoAssid.add("convertRidIDtoAssid");
				convertRidIDtoAssid.add(rid);
				AcademicFrame.myclient.sendMessageToServer(convertRidIDtoAssid);		
				}			
			}
		});
		btnReview.setBounds(444, 86, 91, 23);
		add(btnReview);
		
		btnSave = new JButton("Save");
		btnSave.setEnabled(false);
		btnSave.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (recommendationArea.getText().isEmpty())
				{
					(new Dialog ("Please type your recommendation first.")).setVisible(true);
				}
				else
				{
					setButtons(false);
					btnReview.setEnabled(false);
					discussionArea.setText(discussionArea.getText() + CommonMethods.getDate() + " " + name +"> " + recommendationArea.getText() + "\n" );
					recommendationArea.setText("Please wait while updating the discussion log.");
					ArrayList<Object> discussionLog = new ArrayList<Object>();
					discussionLog.add("setDiscussionLog");
					discussionLog.add(rid);
					discussionLog.add(discussionArea.getText());
					AcademicFrame.myclient.sendMessageToServer(discussionLog);					
				}
			}
		});
		btnSave.setBounds(489, 373, 116, 23);
		add(btnSave);
		
		btnVerdict = new JButton("Verdict");
		btnVerdict.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				myframe.setSize(640, 330);
				myframe.setContentPane( (new VerdictPanel(myframe, myself, rid, committeePanel, name)) );
			}
		});
		btnVerdict.setEnabled(false);
		btnVerdict.setBounds(326, 460, 91, 23);
		add(btnVerdict);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		scrollPane_1.setBounds(58, 348, 403, 73);
		add(scrollPane_1);
		
		recommendationArea = new JTextArea();
		scrollPane_1.setViewportView(recommendationArea);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(57, 145, 403, 157);
		add(scrollPane);
		
		discussionArea = new JTextArea();
		scrollPane.setViewportView(discussionArea);
		discussionArea.setEditable(false);
		
		JLabel lblNewLabel = new JLabel("Discussion log");
		lblNewLabel.setBounds(58, 120, 102, 14);
		add(lblNewLabel);
		
		JLabel lblEnterYourRecommendation = new JLabel("Enter your recommendation");
		lblEnterYourRecommendation.setBounds(58, 323, 170, 14);
		add(lblEnterYourRecommendation);
		
		deadlineArea = new JTextField();
		deadlineArea.setEditable(false);
		deadlineArea.setBounds(326, 432, 91, 20);
		add(deadlineArea);
		deadlineArea.setColumns(10);
		
		lblDeadline = new JLabel("Deadline");
		lblDeadline.setBounds(265, 435, 56, 14);
		add(lblDeadline);
		
		btnExtendDeadline = new JButton("Extend deadline");
		btnExtendDeadline.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				if (CommonMethods.exceededDeadline(deadlineArea.getText()))
				{	
					(new Dialog ("Deadline has passed, cannot extend.")).setVisible(true);
					btnExtendDeadline.setEnabled(false);
				}
				else
					if (CommonMethods.moreThan3Days(deadlineArea.getText()) )
					{
						(new Dialog ("More than 3 days to the deadline, cannot extend.")).setVisible(true);
						
						btnExtendDeadline.setEnabled(false);
					}
					else
					{
						btnReview.setEnabled(false);
						comboBox.setEnabled(false);
						btnSave.setEnabled(false);
						ExtendDeadline extendFrame = new ExtendDeadline(deadlineArea.getText(), btnExtendDeadline, rid, "6", "reviewanddecision", "wasExtended", "reqID" );
						extendFrame.setVisible(true);
						extendFrame.setSize(760,400);
					}
	
			}
		});
		btnExtendDeadline.setEnabled(false);
		btnExtendDeadline.setBounds(444, 432, 124, 23);
		add(btnExtendDeadline);
	}

	/**
	 * This method sets all buttons to be enabled or not, using the variable sent to it.
	 * @param trueORfalse boolean variable 
	 */
	private void setButtons (boolean trueORfalse)
	{
		
		//btnClose.setEnabled(trueORfalse);
	//	btnExtendDeadline.setEnabled(trueORfalse);
		btnSave.setEnabled(trueORfalse);
		if (isChairman)
			btnVerdict.setEnabled(trueORfalse);
		else
			btnVerdict.setEnabled(false);
		btnViewAssessment.setEnabled(trueORfalse);
		btnViewRequest.setEnabled(trueORfalse);

		
	}
	
	@Override
	public void display(Object message)
	{
		
		ArrayList<Object> answer = new ArrayList<Object>();
		answer = (ArrayList<Object>)message;
		
		
		if (	((String)(answer.get(0))).equalsIgnoreCase("reviewableRequests"))
		{
			int numberOfIDs = Integer.parseInt((String)answer.get(1)); // get the amount of items to appear in the combobox
			this.comboBox.addItem("--Choose a request--");
			for (int i=0; i < numberOfIDs ; i++ )
			{
				this.comboBox.addItem((String)answer.get(i+2));  // add requests to the list
			}
		}
		else
			if (	((String)(answer.get(0))).equalsIgnoreCase("ridConveretedToAssid"))
			{
				this.assID = (String) answer.get(1);
				
				ArrayList<Object> getReviewAndDecision = new ArrayList<Object>();
				getReviewAndDecision.add("getReviewAndDecision");
				getReviewAndDecision.add(rid);
				AcademicFrame.myclient.sendMessageToServer(getReviewAndDecision);	
			}
			else
				if ( ((String)(answer.get(0))).equalsIgnoreCase("returnedReviewAndDecision")	)
				{
					
				
					if (	((String)(answer.get(1))).equalsIgnoreCase("Error: No Review and Decision found")	)
					{
						(new Dialog ("Could not find Request for the chosen Assessment")).setVisible(true);
						myframe.setContentPane(committeePanel);
						myframe.setSize(500, 400);
					}
					else
					{
						ReviewAndDecisionEntity reviewAndDecision = (ReviewAndDecisionEntity)answer.get(2); // get the RnD entity
						
						this.discussionArea.setText(reviewAndDecision.getrndLog());
						this.deadlineArea.setText(reviewAndDecision.getdeadline());
							
						setButtons(true);
						if (reviewAndDecision.getwasExtended().equalsIgnoreCase("no"))
							this.btnExtendDeadline.setEnabled(true);	
						else
							this.btnExtendDeadline.setEnabled(false);
						btnReview.setEnabled(true);

					}
				}
				else
					if (	((String)(answer.get(0))).equalsIgnoreCase("discussionLogAnswer")	)
					{	
						if ( ((String)(answer.get(1))).equalsIgnoreCase("Invalid") )
						{
							(new Dialog ("Error: Could not save discussion log to the server.")).setVisible(true);
							myframe.setContentPane(committeePanel);
							myframe.setSize(500, 400);
						}
					
						else
						{
						
							setButtons(true);
							btnReview.setEnabled(true);
							recommendationArea.setText("");
						}
						
					}
					else
						if (	((String)(answer.get(0))).equalsIgnoreCase("extentionRequestWasSent")	)
						{
							btnReview.setEnabled(true);
							comboBox.setEnabled(true);
							btnSave.setEnabled(true);
						}
	
							
		

		 
		
	}
}
